<template>
    <span>
        <a :href="toUri" target="_blank" @click="clickLink">
            {{name}}
        </a>
    </span>
</template>

<script>
    import {Base64} from 'js-base64'
    export default {
        name:'dynamic-link',
        props: {
            name: {
                type: String,
                require: true
            },
            params:{
                type:Object,
                default:()=>{return {}}
            },
            pageTitle:{
                type: String,
                require: true
            },
            component:{
                type:String,
                require: true
            },
            isFull:{
                type:Boolean,
                default:()=>{return false}
            },
            uri:{
                type:String,
                default:()=>{return ''}
            },
            disableClick:{
                type:Boolean,
                default:()=>{return false}
            },
            withSsoToken:{
                type:Boolean,
                default:()=>{return false}
            }
        },
        computed:{
            toUri(){
                if(this.uri!=''){
                    if(this.withSsoToken&&this.$store.getters.sso_access_token!=='') {
                        debugger
                        if (this.uri.indexOf("?") > 0) {
                            return this.uri+"&token="+this.$store.getters.sso_access_token;
                        }else {
                            return this.uri+"?token="+this.$store.getters.sso_access_token;
                        }
                    }
                    return this.uri;
                }else {
                    const token = this.getToken();
                    let data = {
                        token: token,
                        pageData: {
                            params: this.params,
                            component: this.component,
                            isFull: this.isFull,
                            pageTitle: this.pageTitle,
                            eventHashKey:md5(this.$route.name)
                        }
                    };
                    return this.$router.resolve('/d/afsLink').href + '?_link=' + Base64.encode(JSON.stringify(data), true)
                }
           }
        },
        data(){
            return {}
        },
        methods:{
            clickLink(){
                if(this.disableClick){
                    const event = event || window.event;
                    if (event.preventDefault) {
                        event.preventDefault();
                    } else {
                        event.returnValue = false;
                    }
                    return false;
                }
            },
            getToken(){
                return  {
                    accessToken:this.$store.getters.access_token,
                    refreshToken:this.$store.getters.refresh_token,
                    expiresIn:this.$store.getters.expires_in
                }
            }
        }
    }
</script>
